/**
 * 
 */
package core.proto;

import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.text.DefaultHighlighter;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

/**
 * @author poornima
 * 
 */
public class TextHighlighter extends DefaultHighlighter.DefaultHighlightPainter {

	public static Color color_text_highlight = new Color(0,255,0);
	
	public TextHighlighter(Color color) {
		super(color);
	}
	
	public static String highlightAnnotations(String annotation, int index, String document)
	{
		if (document == null || StringUtils.isEmpty(annotation) || document == null
				|| StringUtils.isEmpty(document)) {
			return document;
		}
		if(index > document.length() - 1)
		{
			System.out.println("error occurred while trying to highlight annotation...index was too high");
		}
		String before = document.substring(0,index);
		String after = document.substring(index + annotation.length());
		String highlighted = "<font style=\"background-color:yellow\">" + annotation + "</font>";

		if(before.substring(before.length()-38).equals("<font style=\"background-color:yellow\">") && after.substring(0,7).equals("</font>"))
		{
			highlighted = annotation;
		}
		
		return before+highlighted+after;
	}

	public static String removeAnnotationHighlights(String document)
	{
		int i = 0;
		String revisedDocument = document;
		while(i < revisedDocument.length() && i >= 0)
		{
			int index = revisedDocument.indexOf("<font style=\"background-color:yellow\">",i);
			if(index == -1)
			{
				return revisedDocument;
			}
			int endIndex = revisedDocument.indexOf("</font>",index) + 7;
			if(endIndex == -1)
			{
				System.out.println("error occurred while trying to remove highlights...open font style tag present without closing tag");
				return "";
			}
			i = endIndex;
			String before = revisedDocument.substring(0,index);
			String after = revisedDocument.substring(endIndex);

			String unhighlighted = revisedDocument.substring(index+38,endIndex-7);
			//unhighlight.replace("<font style=\"background-color:yellow\">","");
			//unhighlight.replace("</font>", "");
			revisedDocument = before+unhighlighted+after;
		}
		return revisedDocument;
	}

	public String highlightText(String pattern, String spec) {
		if (pattern == null || StringUtils.isEmpty(pattern) || spec == null
				|| StringUtils.isEmpty(spec)) {
			return "spec";
		}
		StringBuffer replaceFontCol = new StringBuffer(
				"<font style=\"background-color:yellow\">");
		StringBuffer result = new StringBuffer(spec);
		/*
		 * if (spec.contains(pattern)) { replaceFontCol.append(pattern);
		 * replaceFontCol.append("</font>"); spec = spec.replace(pattern,
		 * replaceFontCol.toString()); return spec; }
		 */

		String patternTokens[] = pattern.split(" ");
		if (patternTokens == null || patternTokens.length == 0) {
			return spec;
		}
		List<Integer> indexList = new ArrayList<Integer>();
		boolean isPresent = true;
		int index = 0;
		for (String token : patternTokens) {
			if (!spec.contains(token.trim())) {
				isPresent = false;
				break;
			}
			index = spec.indexOf(token, index);
			indexList.add(index);
		}
		if (!isPresent) {
			return spec;
		} else {
			int indVal = indexList.get(0);
			for (int indexVal : indexList) {
				if (indVal > indexVal) {
					isPresent = false;
					break;
				}
			}
			if (!isPresent) {
				return spec;
			}

			result = new StringBuffer(spec.substring(0, indexList.get(0)));
			result.append("<font style=\"background-color:yellow\">");
			result.append(pattern);
			result.append("</font>");
			result.append(spec.substring(indexList.get(indexList.size() - 1)
					+ patternTokens[patternTokens.length - 1].length()));
		}
		return result.toString();
	}

	public static void main(String args[]) throws IOException {
		TextHighlighter obj = new TextHighlighter(color_text_highlight);
		String spec = FileUtils
				.readFileToString(new File(
						"C:\\Users\\poornima\\Desktop\\CollabVizProject\\SKExperimentMaterials\\SKExperimentMaterials\\Htmls\\1-AllanVerville-Interview.htm"));
		System.out
				.println(obj.highlightText("My name is Allan Verville", spec));

	}
}
